//
//  AppDelegate.swift
//  weibo
//
//  Created by Qing on 2017/4/29.
//  Copyright © 2017年 成哥哥. All rights reserved.
//

import UIKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        
        //测试用户信息模型
        print("用户登录状态\(UserAccountViewModel.sharedUserAccount.userLogon)")
        
        
        //View的范围，UIScreen：屏幕，  bounds：界限
        window = UIWindow(frame: UIScreen.main.bounds)
        
        //View背景颜色
        window?.backgroundColor = UIColor.white
        
        //View的初始控制器
        window?.rootViewController = defaultRootViewControlle
        
        //设置为主窗口并显示
        window?.makeKeyAndVisible()
        
        setupAppearance()
        
        //监听通知
        NotificationCenter.default.addObserver(
        forName: NSNotification.Name(rawValue: "WBSwiftRootViewController"),    //通知名称，通知中心用来识别的
        object: nil,                                                            //发送通知的对象，如果为nil，则监听全部对象
        queue: nil)                                                             //nil，
        { (notification) in
            
            //显示欢迎回来后，切换视图。
//            let vc = notification.object != nil ? wilcomeViewController() : MainViewController()
            print("监听方法")
            let vc = notification.object != nil ? MainViewController() : wilcomeViewController()
            self.window?.rootViewController = vc
//            self.window?.rootViewController = MainViewController()
            
        }
        
        return true
    }
    
    deinit{
        
        NotificationCenter.default.removeObserver(
            self,                                                       //监听执行者
            name: NSNotification.Name(rawValue: "WBSwiftRootViewController"),                          //要销毁的通知名称
            object: nil)                                                //要销毁的对象
    }
    

    /// 全局控件颜色组
    fileprivate func setupAppearance() {
        UINavigationBar.appearance().tintColor = WBappearanceColor
        UITabBar.appearance().tintColor = WBappearanceColor
        
    }

}


extension AppDelegate {
    
//MARK: - 界面切换代码
    fileprivate var defaultRootViewControlle: UIViewController {
        
        //1.判断用户是否登录成功，
        if UserAccountViewModel.sharedUserAccount.userLogon {
            
            //有登录成功，并且有新版本则切换到NewFeaTureViewController, 有登录成功没有新版本则切换到wilcomeViewController
            return isNewVersion ? NewFeaTureViewController() : wilcomeViewController()
            
        }
        //2. 没有登录成功返回主控制器
        return MainViewController()
    }
    
    
    /// 是否有新版本,true为有，false为没有
    fileprivate var isNewVersion: Bool{
        
        //1.当前的版本,版本是保存在info里面的。从info里面提取出来。
        let currentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")! as! String
        //把版本号转换成double类型才可以进行对比
        let version = Double(currentVersion)!
        print("最新的版本\(version)")
        
        //2.之前的版本:把当前版本保存在用户偏好
        let sandboxVersionKey = "sandboxVersionKey"
        
        let sandboxVersion = UserDefaults.standard.double(forKey: sandboxVersionKey)
        print("之前的版本\(sandboxVersion)")
        
        UserDefaults.standard.set(version, forKey: sandboxVersionKey)
        
        //最新的版本大于之前的版本则为真
        return version > sandboxVersion
    }

}
